// Fill out your copyright notice in the Description page of Project Settings.


#include "AnimPlayer/Actions/ActionBase.h"
#include "CurveWrappers/FloatCurveInitParam.h"

FActionBase::FActionBase(): Direction(EAnimDirection::Up), AnimTarget(), ElementId(-1), Timer(0), StartOffset(0),
                            BeginTime(0), EndTime(0), Duration(0),
                            IsTriggered(false),
                            IsCompleted(false)
{
}

FActionBase::~FActionBase()
{
	if (CurveWrapper != nullptr && CurveWrapper.IsValid())
	{
		CurveWrapper->RemoveFromRoot();
	}
}

void FActionBase::OnInit()
{
	if (Curve.IsValid())
	{
		CurveWrapper = NewObject<UFloatCurveWrapper>();
		CurveWrapper->AddToRoot();

		FFloatCurveInitParam InitParam;
		InitParam.InitType = FFloatCurveInitParam::EInitType::InitWithoutChange;
		CurveWrapper->Init(Curve.Get(), InitParam);

		const auto CurveAsset = CurveWrapper->GetCurveAsset();
		CurveWrapper->ChangeTimeRange(CurveAsset, 0, Duration);
	}
}

void FActionBase::OnBegin()
{
	IsTriggered = true;

	Timer = 0;
}

void FActionBase::OnTick(float DeltaTime)
{
	Timer += DeltaTime;
}

void FActionBase::OnEnd()
{
	IsCompleted = true;
}

void FActionBase::OnExit()
{
	if (CurveWrapper != nullptr && CurveWrapper.IsValid())
	{
		CurveWrapper->RemoveFromRoot();
		CurveWrapper = nullptr;
	}
}
